package com.rc.evcharger.service.ugs;

import com.rc.evcharger.common.constant.SocketConstant;
import com.rc.evcharger.common.constant.level.UserLevelConstant;
import com.rc.evcharger.mapper.tenant.TenantOpenServiceMapper;
import com.rc.evcharger.mapper.ugs.*;
import com.rc.evcharger.model.order.OrderInfo;
import com.rc.evcharger.model.ugs.TenantUgsLevelDiscountStation;
import com.rc.evcharger.model.ugs.TenantUgsMemberLevel;
import com.rc.evcharger.vo.cost.UserLevelPriceResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * Create by Yang on 2021/5/22
 */
@Service
public class TenantUgsMemberLevelServiceImpl implements TenantUgsMemberLevelService {

    @Autowired
    private TenantUgsMemberLevelMapper tenantUgsMemberLevelMapper;

    @Autowired
    private TenantUgsLevelDiscountStationMapper tenantUgsLevelDiscountStationMapper;

    @Autowired
    private TenantOpenServiceMapper tenantOpenServiceMapper;

    @Autowired
    private TenantUgsLevelGainSettingMapper tenantUgsLevelGainSettingMapper;

    @Autowired
    private TenantUgsLevelGainMapper tenantUgsLevelGainMapper;

    @Autowired
    private UgsMemberSuperLevelMapper ugsMemberSuperLevelMapper;

    @Override
    public TenantUgsMemberLevelMapper getMapper() {
        return tenantUgsMemberLevelMapper;
    }

    /**
     * 获取最优方案:会员价、超级会员价
     *
     * @param order
     * @return
     */
    @Override
    public UserLevelPriceResult selectBestByesIdAndMemberId(OrderInfo order) {

        TenantUgsLevelDiscountStation levelDiscountStation = null;
        TenantUgsLevelDiscountStation svipDiscountStation = null;

        //TODO 是否开通会员体系、超级会员的功能  使用状态(0:开通,1:停止) 默认 0
        String levelStatus0 = tenantOpenServiceMapper.selectStatusByTenantAndOpenServiceCode(order.getTenantCode(), UserLevelConstant.SERVICE_CODE_LEVEL);
        if (SocketConstant.STR_CON0.equals(levelStatus0)) {
            //是否开通对应权益  状态 0启用 1停用 默认0
            //String gainStatusLevel = tenantUgsLevelGainSettingMapper.selectStatusByTenantCodeAndServiceCode(order.getTenantCode(), UserLevelConstant.SERVICE_CODE_L);
            String gainStatusLevel = tenantUgsLevelGainMapper.selectStatusByTenantCodeAndServiceCode(order.getTenantCode(), UserLevelConstant.SERVICE_CODE_L);
            if (SocketConstant.STR_CON0.equals(gainStatusLevel)) {

                //查找对应的会员等级
                TenantUgsMemberLevel tenantUgsMemberLevel = tenantUgsMemberLevelMapper.selectByPrimaryKey(order.getMemberId());
                //查询会员优惠价
                levelDiscountStation = tenantUgsLevelDiscountStationMapper.selectValidByEsIdAndType(order.getTenantCode(), tenantUgsMemberLevel.getLevelType(), order.getElecStationId());
            }
        }

        //超级会员查询权益
        String svipStatus0 = tenantOpenServiceMapper.selectStatusByTenantAndOpenServiceCode(order.getTenantCode(), UserLevelConstant.SERVICE_CODE_SVIP);
        if (SocketConstant.STR_CON0.equals(svipStatus0)) {
            //是否开通对应权益  状态 0启用 1停用 默认0
            //String gainStatusSvip = tenantUgsLevelGainSettingMapper.selectStatusByTenantCodeAndServiceCode(order.getTenantCode(), UserLevelConstant.SERVICE_CODE_V);
            String gainStatusSvip = tenantUgsLevelGainMapper.selectStatusByTenantCodeAndServiceCode(order.getTenantCode(), UserLevelConstant.SERVICE_CODE_V);
            if (SocketConstant.STR_CON0.equals(gainStatusSvip)) {


                //查询超级会员价
                svipDiscountStation = tenantUgsLevelDiscountStationMapper.selectSuperValidByEsIdAndType(order.getTenantCode(),
                        order.getMemberId(), order.getElecStationId());
            }
        }

        if (levelDiscountStation == null && svipDiscountStation == null) {
            return UserLevelPriceResult.emptyResult();
        }

        //返回LEVEL
        if (levelDiscountStation != null && svipDiscountStation == null) {
            UserLevelPriceResult userLevelPriceResult = new UserLevelPriceResult();
            userLevelPriceResult.setTenantCode(levelDiscountStation.getTenantCode());
            userLevelPriceResult.setType(UserLevelConstant.USER_LEVEL_PRICE);
            userLevelPriceResult.setDiscountPrice(levelDiscountStation.getDiscountRatio());
            userLevelPriceResult.setId(levelDiscountStation.getDiscountStationId());

            //计算优惠服务费金额
            BigDecimal saleTotalServiceMoney = order.getTotalSeviceMoney().multiply(levelDiscountStation.getDiscountRatio()).setScale(2, BigDecimal.ROUND_HALF_UP);
            userLevelPriceResult.setDiscountAmount(order.getTotalSeviceMoney().subtract(saleTotalServiceMoney));
            return userLevelPriceResult;  //返回服务费折扣
        }

        //返回VIP
        if (levelDiscountStation == null && svipDiscountStation != null) {
            UserLevelPriceResult userLevelPriceResult = new UserLevelPriceResult();
            userLevelPriceResult.setTenantCode(svipDiscountStation.getTenantCode());
            userLevelPriceResult.setType(UserLevelConstant.USER_LEVEL_PRICE_SVIP);
            userLevelPriceResult.setDiscountPrice(svipDiscountStation.getDiscountRatio());
            userLevelPriceResult.setId(svipDiscountStation.getDiscountStationId());

            //计算优惠服务费金额
            BigDecimal saleTotalServiceMoney = order.getTotalSeviceMoney().multiply(svipDiscountStation.getDiscountRatio()).setScale(2, BigDecimal.ROUND_HALF_UP);
            userLevelPriceResult.setDiscountAmount(order.getTotalSeviceMoney().subtract(saleTotalServiceMoney));
            return userLevelPriceResult;  //返回服务费折扣
        }

        /**比较最优 相等情况下，按优惠方案价>超级会员>企业折扣价>会员等级价*/
        if (levelDiscountStation.getDiscountRatio().compareTo(svipDiscountStation.getDiscountRatio()) >= 0) {
            //返回VIP
            UserLevelPriceResult userLevelPriceResult = new UserLevelPriceResult();
            userLevelPriceResult.setTenantCode(svipDiscountStation.getTenantCode());
            userLevelPriceResult.setType(UserLevelConstant.USER_LEVEL_PRICE_SVIP);
            userLevelPriceResult.setDiscountPrice(svipDiscountStation.getDiscountRatio());
            userLevelPriceResult.setId(svipDiscountStation.getDiscountStationId());

            //计算优惠服务费金额
            BigDecimal saleTotalServiceMoney = order.getTotalSeviceMoney().multiply(svipDiscountStation.getDiscountRatio()).setScale(2, BigDecimal.ROUND_HALF_UP);
            userLevelPriceResult.setDiscountAmount(order.getTotalSeviceMoney().subtract(saleTotalServiceMoney));
            return userLevelPriceResult;  //返回服务费折扣
        } else {
            //返回LEVEL
            UserLevelPriceResult userLevelPriceResult = new UserLevelPriceResult();
            userLevelPriceResult.setTenantCode(levelDiscountStation.getTenantCode());
            userLevelPriceResult.setType(UserLevelConstant.USER_LEVEL_PRICE);
            userLevelPriceResult.setDiscountPrice(levelDiscountStation.getDiscountRatio());
            userLevelPriceResult.setId(levelDiscountStation.getDiscountStationId());

            //计算优惠服务费金额
            BigDecimal saleTotalServiceMoney = order.getTotalSeviceMoney().multiply(levelDiscountStation.getDiscountRatio()).setScale(2, BigDecimal.ROUND_HALF_UP);
            userLevelPriceResult.setDiscountAmount(order.getTotalSeviceMoney().subtract(saleTotalServiceMoney));
            return userLevelPriceResult;  //返回服务费折扣
        }
    }
}
